package com.xaicode.template.batch.job.file;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.MalformedURLException;

/**
 * @author beborn
 * @date 2021/12/31 0031 15:13
 */
@Component
public class FileItemReaderDemo {

    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job fileItemReaderJob() throws MalformedURLException {
        return jobBuilderFactory.get("fileItemReaderJob")
                .start(step())
                .build();
    }

    private Step step() throws MalformedURLException {
        return stepBuilderFactory.get("step")
                .<TransData, TransData>chunk(2)
                .reader(fileItemReader())
                .writer((ItemWriter<Object>) items ->
                        items.forEach(System.out::println))
                .build();
    }

    private ItemReader<TransData> fileItemReader() throws MalformedURLException {

//                FlatFileItemReader<TransData> build = new FlatFileItemReaderBuilder<TransData>()
//                .name("")
//                .resource(new PathResource(""))
//                .lineMapper(mapper)
//                .linesToSkip(1)
//                .lineTokenizer(tokenizer)
//                .build();

        FlatFileItemReader<TransData> reader = new FlatFileItemReader<>();
//        reader.setResource(new PathResource("classpath:file/jobFile"));
        reader.setResource(new ClassPathResource("classpath:file/jobFile"));
        // Public setter for the number of lines to skip at the start of a file.
        reader.setLinesToSkip(1);

        // 创建分词器以固定分隔符处理数据读取，默认构造器使用都好为分隔符
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        // 设置属性名，类似表头
        tokenizer.setNames("id", "col1", "col2", "col3");
        // 将每行数据转换为指定对象
        DefaultLineMapper<TransData> mapper = new DefaultLineMapper<>();
        // 设置分词器
        mapper.setLineTokenizer(tokenizer);
        // 设置映射方式
        mapper.setFieldSetMapper(fieldSet -> {
            TransData transData = new TransData();
            transData.setId(fieldSet.readInt("id"));
            transData.setCol1(fieldSet.readString("col1"));
            transData.setCol2(fieldSet.readString("col2"));
            transData.setCol3(fieldSet.readString("col3"));
            return transData;
        });

        reader.setLineMapper(mapper);

        // TODO In strict mode the reader will throw an exception if the input resource does not exist.
        reader.setStrict(false);

        return reader;
    }

}
